export default {
  changeUrlArg(url, arg, val) {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + val;
    return url.match(pattern) ? url.replace(eval('/(' + arg + '=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url + '&' + replaceText : url + '?' + replaceText);
  },
  queryToJson(url) {
    let arr = url.split("?");
    if (arr.length == 2) {
      let parameters = arr[1].split("&");
      let obj = {};
      for (let i of parameters) {
        obj[i.split("=")[0]] = i.split("=")[1];
      }
      return obj;
    } else {
      return {};
    }
  },
  addSessionParameter(key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
  },
  getSessionParameter(key) {
    return JSON.parse(sessionStorage.getItem(key));
  },
  dateFtt(fmt, date) {
    var o = {
      "M+": date.getMonth() + 1,     //月份
      "d+": date.getDate(),     //日
      "h+": date.getHours(),     //小时
      "m+": date.getMinutes(),     //分
      "s+": date.getSeconds(),     //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds()    //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  startsWith(searchString, positionStr) {
    if (positionStr == null || positionStr == "" || searchString.length == 0 || positionStr.length > searchString.length)
      return false;
    if (searchString.substring(0, positionStr.length) == positionStr)
      return true;
    else
      return false;
  }
}

